#include "stdafx.h"

GLuint CreateShader(GLenum type, const char* pSource)
{
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &pSource, NULL);
    glCompileShader(shader);
    //printShaderInfoLog(shader);
    return shader;
}

bool CheckShader(GLint shader)
{
	GLint length;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
    if (length)
    {
        //char* buffer = (char*)malloc( sizeof(char) * length ) ;
       // glGetShaderInfoLog(shader, length, NULL, buffer);
        //printf("%s", buffer);
        //free( buffer ) ;
        GLint success;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if(success != GL_TRUE)
		{
            return false;
        }
    }

	return true;
}

bool CompileShaders()
{
	const char* vertexSrc = "#version 330\nin vec4 position; in vec4 inColor; out vec4 vColor; void main() { gl_Position = position; vColor = inColor; }";
	const char* fragmentSrc = "#version 330\nin vec4 varColor; out vec4 frColor; void main() { frColor = varColor; }";

	
    GLuint shaderProgram = glCreateProgram();

    GLuint vertexShader = CreateShader(GL_VERTEX_SHADER, vertexSrc);
	if(CheckShader(vertexShader) == false)
	{
		return false;
	}

    GLuint fragmentShader = CreateShader(GL_FRAGMENT_SHADER, fragmentSrc);
	if(CheckShader(fragmentShader) == false)
	{
		return false;
	}

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);

    glLinkProgram(shaderProgram);

    glUseProgram(shaderProgram);

	return true;
}

